package com.leetcode.No0413;

import org.junit.Test;

/**
 * @program: leetcode
 * @description: 等差数列划分
 * @author: wangzhihua
 * @date: 2021-08-20
 */
public class Solution01 {

	@Test
	public void test01() {
		int[] nums = new int[]{1,2,3,4};
		System.out.println(numberOfArithmeticSlices(nums));
	}

	@Test
	public void test02() {
		int[] nums = new int[]{7,7,7,7};
		System.out.println(numberOfArithmeticSlices(nums));
	}

	public int numberOfArithmeticSlices(int[] nums) {
		int len = nums.length;
		if (len < 3) {
			return 0;
		}
		int ans = 0;
		int left = 0, right = 0;
		int interval = 0;
		while (right < len) {
			if (right + 1 == len) {
				ans += calSum(right - left - 1);
				break;
			}
			if (interval == nums[right + 1] - nums[right]) {
				++right;
			} else {
				ans += calSum(right - left - 1);
				left = right;
				++right;
				interval = nums[right] - nums[left];
			}
		}
		return ans;
	}

	private int calSum(int n) {
		if (n <= 0) {
			return 0;
		}
		int sum = 0;
		while (n > 0) {
			sum += n--;
		}
		return sum;
	}

}
